package com.example.druid;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

@Configuration
public class DataSourceConfig {

    /**
     * 本地读取配置 远程读取配置跟这个差不多
     * @return
     */
//    @Bean
//    public DataSource dataSource(){
//        DruidDataSource druidDataSource = new DruidDataSource();
//        try {
//            druidDataSource.setFilters("config");
//            Properties properties = new Properties();
//            File file = new File("/Users/shanchong/work/project/demo/src/main/java/com/example/druid/druid-pool.properties");
//            properties.load(new FileInputStream(file));
//            druidDataSource.setConnectProperties(properties);
//        } catch (SQLException e) {
//            e.printStackTrace();
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return druidDataSource;
//    }

    /**
     *  密码加密
     *  privateKey:MIIBVQIBADANBgkqhkiG9w0BAQEFAASCAT8wggE7AgEAAkEAsFqIHA7MvzSMZo0amG8zxsthzWs7/KHJm2jZHxoiTaCVFg1seya28jxkl6b6rstJTCtLrJf99PANoNOb+4Zl0QIDAQABAkEApU4HHX3F8EiafpovXsmF2AbWg03MKe+hYfpy4xOFKgsWL70I/Ualv8ipPKeiCTaPDp2QYW8h81TT3rpgHSsOAQIhAO30Dqx+fWwE8+JCOdt9fLIfhqWUtiqUiqk4s9gYIJXhAiEAvbp+5iZLPcXuD/yI3EvDNv4vC0mLrIkcfTD+gK0Y7fECICsMC9uIxofUHYrBEFZ0zSosKd+ql4n8dVlHuMFPHBYBAiAnBAP2hidw45QKG491ZesWEv1Pn+cmKuf94qZjhbUbsQIhAIkfx5OL4xoSN8Sh5+H+iiiU6wtvf53jYGxlf0R1L5Gk
     *  publicKey:MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBALBaiBwOzL80jGaNGphvM8bLYc1rO/yhyZto2R8aIk2glRYNbHsmtvI8ZJem+q7LSUwrS6yX/fTwDaDTm/uGZdECAwEAAQ==
     *  password:O5UQtf+DKHyGNu0zc9YjW6ERzcDOP23PDhSfYbG+BrRpXwgHtJ6uaZmJ3hLsbtfbOj4uGh2CRgQwWGnNvWmFuQ==
     *
     * @return
     */
   @Bean
    public DataSource dataSource(){
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setUrl("jdbc:mysql://127.0.0.1:3306/demo");
        druidDataSource.setUsername("root");
        druidDataSource.setPassword("O5UQtf+DKHyGNu0zc9YjW6ERzcDOP23PDhSfYbG+BrRpXwgHtJ6uaZmJ3hLsbtfbOj4uGh2CRgQwWGnNvWmFuQ==");
        Properties properties = new Properties();
        File file = new File("/Users/shanchong/work/project/demo/src/main/java/com/example/druid/druid-pool.properties");
        try {
            properties.load(new FileInputStream(file));
        } catch (IOException e) {
            e.printStackTrace();
        }
        druidDataSource.setConnectProperties(properties);
        return druidDataSource;
    }


}
